package cn.hfjava.kz.controller;

import cn.dev33.satoken.util.SaResult;
import cn.hfjava.kz.entity.po.Role;
import cn.hfjava.kz.entity.po.User;
import cn.hfjava.kz.mapper.PermissionMapper;
import cn.hfjava.kz.mapper.RoleMapper;
import cn.hfjava.kz.service.RoleService;
import cn.hfjava.kz.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/role")
@Tag(name = "角色管理模块", description = "角色管理模块接口")
public class RoleController {

    private final RoleService roleService;


    private final UserService userService;

    private final PermissionMapper permissionMapper;

    private final RoleMapper roleMapper;

    //新增角色
    @PostMapping("/add")
    @Operation(summary = "新增角色")
    public SaResult addRole(@RequestBody Role role) {
        roleService.save(role);
        return SaResult.ok("新增成功！");
    }

    //删除角色、角色如有分配不能删
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除角色")
    public SaResult deleteRole(@PathVariable Integer id) {
        List<User> users = roleService.isExistsByRoleId(id);
        if (users.isEmpty()) {
            roleService.removeById(id);
            return SaResult.ok("删除成功！");
        } else {
            return SaResult.error("该角色已分配用户，不能删除！");
        }
    }


    //根据角色查询用户
    @GetMapping("/users/{roleId}")
    @Operation(summary = "根据角色ID查询用户")
    public SaResult findUsersByRoleId(@PathVariable Integer roleId) {
        List<User> users = userService.findUsersByRoleId(roleId);
        for(User user : users){
            user.setPassword("");
        }
        return SaResult.ok("查询成功！").setData(users);
    }


    //更新角色名称
    @PutMapping("/update")
    @Operation(summary = "更新角色名称")
    public SaResult updateRoleName(@RequestBody Role role) {
        if (roleService.getById(role.getId()) == null) {
            return SaResult.error("该角色不存在");
        } else {
            roleService.updateById(role);
            return SaResult.ok("更新成功！");
        }
    }


    //查询角色列表
    @GetMapping("/list")
    @Operation(summary = "查询角色列表")
    public SaResult list() {
        List<Role> roles = roleService.list();
        return SaResult.ok("查询成功!").setData(roles);
    }

    //为角色分配相应的权限(批量插入)
    @PostMapping("/bindPermission")
    @Operation(summary = "为角色分配权限")
    public SaResult bindPermission(@RequestParam Integer roleId, @RequestBody Integer[] permissionId) {
        if (roleId == null || permissionId == null) {
            return SaResult.error("id不能为空！");
        } else if (roleService.getById(roleId) == null) {
            return SaResult.error("该角色不存在！");
        }
        for (Integer permId : permissionId) {
            if (permissionMapper.selectById(permId) == null) {
                return SaResult.error("该权限不存在！");
            } else {
                roleMapper.bindPermission(roleId, permId);
            }
        }
        return SaResult.ok("绑定成功！");
    }



    //查询角色列表
    @GetMapping("/NewList")
    @Operation(summary = "查询角色列表")
    public SaResult NewList() {
        List<Role> roles = roleService.findRoleList();
        return SaResult.ok("查询成功!").setData(roles);
    }


    @Operation(summary = "查询角色及权限信息")
    @GetMapping("/get/{id:\\d+}")
    public SaResult getRole(@PathVariable(value = "id") Integer roleId) {
        Role roleVo = roleService.findRoleById(roleId);
        return SaResult.data(roleVo);
    }
}
